<?php

namespace app\model;

class DeviceData extends BaseModel
{
    public function search(array $search)
    {
        $condition = [];
        $startTime = $search['start_time'] ?? '';
        $endTime = $search['end_time'] ?? time();

        $startTime = 10 < strlen($startTime) ? ceil($startTime / 1000) : $startTime;
        $endTime = 10 < strlen($endTime) ? ceil($endTime / 1000) : $endTime;
        if ($startTime) {
            $condition[] = ['receive_time', 'between', [$startTime, $endTime]];
        }

        return $this->where($condition);
    }

    public function getReceiveTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
}